package celcatSynchronizer;

import celcatSynchronizer.ui.FrameMain;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import util.Config;
import util.Log;
import util.VersionNumber;
import util.codecs.Codec;
import util.codecs.CodecInputStreamConfigIni;
import util.codecs.CodecOutputStreamConfigIni;

/**
 * Class principale.
 */
public class CelcatSynchronizer
{
	public static final VersionNumber version = new VersionNumber(1, 0);
	public static final String configFileName = "config.ini";
	public static Config config;
	public static Log log;
	
	/**
	 * Point d'entrée de l'application.
	 * @param p_args Arguments de la ligne de commande.
	 */
	public static void main(String[] p_args)
	{
		// Création de l'objet de configuration.
		Config.registerCodec("ini", new Codec<Config>(CodecInputStreamConfigIni.class, CodecOutputStreamConfigIni.class));
		config = new Config();
		config.setProperty("startWeek", String.valueOf(new GregorianCalendar().getTimeInMillis()));
		config.setProperty("lastLogin", "");
		
		Config configLog = new Config();
		configLog.setProperty("stream", "log.txt");
		configLog.setProperty("level", "Warning");
		configLog.setProperty("append", "false");
		config.setSection("Log", configLog);
		
		try
		{
			// Chargement de la configuration du fichier.
			config.merge(Config.load(configFileName));
		}
		catch(Exception ex)
		{
			try
			{
				// Si le fichier n'existe pas, on tente de le créer.
				Config.store(configFileName, config);
			}
			catch(Exception exc)
			{
				log.warning("Can't create configuration file");
				log.debug(exc);
			}
		}
		
		try
		{
			// Création du log.
			String logStreamString = configLog.getProperty("stream");
			OutputStream logStream = System.out;
			if(!logStreamString.equals("System.out"))
				logStream = new FileOutputStream(new File(logStreamString), Boolean.parseBoolean(configLog.getProperty("append")));
			
			String logLevelString = configLog.getProperty("level");
			Log.Level logLevel = Log.Level.Warning;
			if(logLevelString.equals("Fatal"))
				logLevel = Log.Level.Fatal;
			else if(logLevelString.equals("Error"))
				logLevel = Log.Level.Error;
			else if(logLevelString.equals("Warning"))
				logLevel = Log.Level.Warning;
			else if(logLevelString.equals("Notice"))
				logLevel = Log.Level.Notice;
			else if(logLevelString.equals("Debug"))
				logLevel = Log.Level.Debug;
			
			log = new Log(logStream, logLevel);
		}
		catch(Exception ex)
		{
			log = new Log(System.out, Log.Level.Warning);
			log.warning("An exception occured while opening log file");
		}
		
		try
		{
			// Chargement du JDBC SQLite.
			Class.forName("org.sqlite.JDBC");
		}
		catch(ClassNotFoundException ex)
		{
			JOptionPane.showMessageDialog(
				null,
				"Une erreur est survenue durant le chargement du driver SQLite",
				"Erreur",
				JOptionPane.ERROR_MESSAGE
			);
			
			log.fatal("An exception occured while loading SQLite driver");
			log.debug(ex);
		}
		try
		{
			// Mise en place du look&feel par défaut du système.
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception ex)
		{
			log.warning("An exception occured while setting the look&feel");
			log.debug(ex);
		}
		
		// Création de la frame principale.
		FrameMain frameMain = new FrameMain();
		frameMain.setDefaultCloseOperation(FrameMain.DISPOSE_ON_CLOSE);
		frameMain.setLocationRelativeTo(null);
		frameMain.setVisible(true);
	}
}
